<template>
	<view class="sss" style=",z-index: -2;position: absolute;">
		<!-- <view class="" style="width: 100%;height: 100%;"> -->
		<image src="../../static/infobj.png" mode="" style="width: 100%;height: 100%;z-index: -1;position: absolute;">
		</image>
		<!-- </view> -->

		<view class="" style="z-index: 1;">

			<view class="" style="display: flex;flex-direction: column;padding-left: 20px;padding-top: 24px;">
				<text style="font-size: 37px;color: #4A5DFF;font-weight: bold;margin-top: 10px;">填写资料</text>


			</view>



			<view class="" style=" margin-left: 20px;margin-right: 20px;border-radius: 6px;background-color: #FFFFFF;">
				<pickers :defaultAddress="['浙江省', '杭州市', '上城区']" @address="address" @close="close">

					<view class=""
						style="display: flex; justify-content: space-between;background-color: #FFFFFF;height: 44px;align-items: center;padding-left: 16px;padding-right: 16px;;border-radius: 6px;">
						<text>工作所在地</text>
						<text style="color: #333333;font-size: 14px;">{{addressTitle}}</text>
					</view>
				</pickers>

				<view class="" style="background-color: #E4E4E4;height: 1px;margin-left:  15px;margin-right: 15px;" />

				<view class="" v-for="(model,index) in danxuanBtn">
					<view class="" style="margin-top: 10px;margin-left: 16px;margin-bottom: 12px;">
						<text>{{model.name}}</text>
					</view>

					<view class="filter-body-section-body style-flex style-flex-wrap" style="padding-left: 25px;">
						<axb-check-box ref="radio3" :list="model.itemList" @change="radioChangeType"></axb-check-box>
					</view>

				</view>

				<!-- <view class="" style="margin-top: 10px;margin-left: 16px;margin-bottom: 12px;">
					<text>个人资产（多选）</text>
				</view>
				<view class="filter-body-section-body style-flex style-flex-wrap" style="padding-left: 25px;">
					<axb-check-box ref="radio1" :multi="true" :list="assets" @change="radioChangeStatus">
					</axb-check-box> 
				</view>-->
			</view>






		</view>
		<view class="" style="background-color: #FFFFFF; margin-top: 18x;margin: 20px;border-radius: 6px;">
			<view class="" style="display: flex;align-items: center;padding: 10px;">
				<image style="width: 24px;" src="../../static/baozhang.png" mode="widthFix"></image>
				<text style="font-size: 18px;color: #333;font-weight: 500;">身份信息</text>
				<text style="font-size: 14px;color: #666;margin-left: 4px;">请填写真实资料</text>
			</view>
			<view class=""
				style="display: flex;flex-direction: row;justify-content: space-between;height: 50px;align-items: center;padding-left: 16px;padding-right: 16px;">
				<text>姓名</text>
				<input type="text" value="" @input="onKeyNameInput" v-model="username" placeholder="请输入您的姓名"
					style="text-align: end;" />
			</view>
			<view class="" style="background-color: #E4E4E4;height: 1px;margin-left:  15px;margin-right: 15px;" />

			<view class=""
				style="display: flex;flex-direction: row;justify-content: space-between;height: 50px;align-items: center;padding-left: 16px;padding-right: 16px;">
				<text>身份证号</text>
				<input type="text" value="" v-model="idCard" placeholder="请输入您的身份证号" style="text-align: end;" />
			</view>
		</view>

		<view class="" @click="postInfo()"
			style="display: flex;flex-direction: row;justify-content: center;margin-top: 10px;">
			<image src="../../static/infobtn.png" mode="" style="width: 355px; height: 85px;"></image>
		</view>

		<!-- <view class="" @click="postInfo()" style="display: flex;flex-direction: row;justify-content: center;">
			<text style="font-size: 12px;color: #999999;">国家级数据加密，仅用于贷款</text>
		</view> -->

		<view class="" style="height: 40rpx;">

		</view>

		<protocol ref="protocolRef"> </protocol>



	</view>


</template>

<script>
	import axbCheckBox from '../../components/axb-checkbox/axb-checkbox.vue'
	import pickers from "@/components/ming-picker/ming-picker.vue"
	import qs from 'qs'
	import protocol from './protocol.vue'

	export default {
		data() {
			return {
				danxuanBtn: [],
				selDanxuan: {},
				assets: [],
				username: '',
				idCard: '',
				loanAmount: '',
				zhimafenValue: '',
				width: 0,
				height: 0,
				addressTitle: '请选择城市',
				chooseAssets: false,
				provinceCode: '',
				cityCode: '',
			}
		},
		onLoad() {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight

			// #ifdef H5
			const getLocalAddress = () => {
				const mapObj = new AMap.Map('iCenter');
				mapObj.plugin('AMap.Geolocation', () => {
					const geolocation = new AMap.Geolocation({
						enableHighAccuracy: true, //是否使用高精度定位，默认:true
						GeoLocationFirst: true,
						timeout: 10000, //超过10秒后停止定位，默认：无穷大
						maximumAge: 0, //定位结果缓存0毫秒，默认：0
						convert: true, //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
					});
					mapObj.addControl(geolocation);
					geolocation.getCurrentPosition();
					AMap.event.addListener(geolocation, 'complete', (res) => {
						const {
							lat,
							lng
						} = res.position
						const geocoder = new AMap.Geocoder({
							radius: 1000 //范围，默认：500
						});
						console.log('获取经纬度---', [lng, lat])
						geocoder.getAddress([lng, lat], (status, result) => {
							if (status === 'complete' && result.regeocode) {
								const address = result.regeocode.formattedAddress;
								console.log(address, result.regeocode)
								const {
									adcode,
									province,
									city,
									district
								} = result.regeocode.addressComponent
								const addressList = [province, city, district]
								if (this.addressTitle === '请选择城市') {
									this.addressTitle = addressList.filter(item => !!item).join('-')
									this.provinceCode = (adcode && adcode.length >= 2) ? adcode.slice(0, 2) : '';
									this.cityCode = (adcode && adcode.length >= 4) ? adcode.slice(0, 4) : '';
								}
								console.log(addressList, this.provinceCode, this.cityCode)
							} else {
								console.error('根据经纬度查询地址失败')
							}
						});
					}); //返回定位信息
					AMap.event.addListener(geolocation, 'error', () => {
						console.log('获取位置失败')
					}); //返回定位出错信息
				});

			}
			if (window.AMap && window.AMap.Geocoder) {
				getLocalAddress()
			} else {
				const scriptDom = document.createElement('script')
				scriptDom.src = 'https://webapi.amap.com/maps?v=1.4.15&key=bd71492679f83a2214787eaeda4891ba&plugin=AMap.Geocoder'
				scriptDom.onload = () => {
					getLocalAddress()
				}
				document.querySelector('head').append(scriptDom)
			}

			// #endif

			this.getInfo()

		},
		onReady() {
			let systemInfo = uni.getSystemInfoSync()
			this.width = systemInfo.windowWidth
			this.height = systemInfo.windowHeight

		},

		methods: {
			address(e) {
				this.addressTitle = e.value.join('-')

				this.provinceCode = e.code[0]
				this.cityCode = e.code[1]
			},

			radioChangeType(model) {
				// console.log(val) // 单选时 返回选中项的value, 反选返回null
				// this.zhimafenValue = val
				// console.log(model)
				if (model.includes('null')) {
					var s = model.substring(4, model.length)
					var ss = s.split('-')[0]
					this.$delete(this.selDanxuan, ss)
				} else {
					var obj = {}; //或者 var obj=new Object();
					var key = model.split('-')[0]
					var value = model.split('-')[1]
					obj[key] = value;
					Object.assign(this.selDanxuan, obj)
				}
				// console.log(this.selDanxuan)
			},
			radioChangeStatus(val) {
				// console.log(val) // 多选时 返回选中项的value数组, 反选返回[]
				if (val[0] == 'clean') {
					//改变check 值
					this.assets.forEach((e) => {

						e.check = 0


					})
					this.chooseAssets = true


				} else {
					//改变check 值
					this.assets.forEach((e) => {
						val.forEach((value) => {
							if (e.value == value) {
								e.check = 1
							}
						})

					})


				}





			},
			cleanAssets() {

				this.assets.forEach((e) => {
					// console.log(e)
					if (e.name != '以上都没有') {
						e.checked = 1;
					}
				})
			},
			async getInfo() {
				const res = await this.$myRequest({
					url: 'api/member/getBaseInfo',
					method: 'post',
					data: {

					}
				});
				res.singleDataList = res.singleDataList.filter(item => {
					if(item.name == '有房'|| item.name == '有车') return true;
				})
				res.multDataList =  res.multDataList.filter(item => {
					if(item.name=='有房'|| item.name == '有车'||item.name == '贷款额度'){
						return false
					}
					return true
				})
					let ss = res.multDataList
				ss.forEach((e) => {

					e.itemList.forEach((eson) => {

						let zhima = Object.assign(eson, {
							'check': 0
						})
						// eson.push(zhima)
						eson.value = e.value + '-' + eson.value
					})

					this.danxuanBtn.push(e)

				})
				// console.log(this.danxuanBtn)
				let assetsArr = res.singleDataList
				assetsArr.forEach((e) => {
					let zhima = Object.assign(e, {
						'check': 0
					})
					this.assets.push(zhima)
				})
				let nothing = {
					'name': '以上都没有',
					'value': 'clean',
					'check': 0
				}
				this.assets.push(nothing)

			},
			//限制只能输入汉字
			onKeyNameInput: function(event) {
				var value = event.target.value;



				if (!value || value == " ") {
					return '';
				}
				const rule = /[^\u4E00-\u9FA5]/g;
				this.$nextTick(function() {

					this.username = value.replace(rule, '');
					// console.log(this.name)



				})

			},
			async postInfo() {
				let infoMap = {}

				Object.assign(infoMap, {
					'username': this.username
				})
				Object.assign(infoMap, {
					'idCard': this.idCard
				})

				// Object.assign(infoMap, {
				// 	'zhimaScore': this.zhimafenValue
				// })
				Object.assign(infoMap, {
					'address': this.addressTitle
				})
				this.assets.forEach((e) => {
					if (e.check == 1) {
						this.chooseAssets = true;
					}
					if (e.value != 'clean') {
						var obj = {}; //或者 var obj=new Object();
						var key = e.value;
						var value = e.check
						obj[key] = value;

						Object.assign(infoMap, obj)
					}
				})
				Object.assign(infoMap, this.selDanxuan)
				if (this.username == '' || this.idCard == '') {
					uni.showToast({
						title: '请完善身份信息',
						icon: 'error'
					})
					return;
				}
				if (this.idCard.length != 18) {
					uni.showToast({
						title: '请输入正确的身份证信息',
						icon: 'error'
					})
					return;
				}

				if (this.addressTitle == '请选择城市') {
					uni.showToast({
						title: '请选择城市',
						icon: 'error'
					})
					return;
				}
				console.log(this.selDanxuan, this.danxuanBtn)
				if (Object.keys(this.selDanxuan).length != this.danxuanBtn.length) {
					uni.showToast({
						title: '请完善相关资料',
						icon: 'error'
					})
					return;
				}
				Object.assign(infoMap, {
					'provinceCode': this.provinceCode
				})
				Object.assign(infoMap, {
					'cityCode': this.cityCode
				})
				const resGetUserInfo = await this.$myRequest({
					url: 'api/member/getUserInfo',
					method: 'post',
					data: {}
				});
				const tokenMobile = resGetUserInfo.mobile;
				if (tokenMobile.length !== 11) {
					uni.setStorageSync('token_mobile', "");
					uni.setStorageSync('token', '')
					uni.setStorageSync('islogin', false)
					const browserParameterStr = sessionStorage.getItem('BrowserAddressParameter')
					const browserParameter = browserParameterStr ? JSON.parse(browserParameterStr) : {}
					delete browserParameter.uid;
					delete browserParameter.u;
					sessionStorage.setItem('BrowserAddressParameter', JSON.stringify(browserParameter))
					uni.redirectTo({
						url: '/pages/index/index/?' + qs.stringify({
							...browserParameter,
							uid: undefined
						})
					})
					// #ifdef H5
					window.location.href = `/#/?${qs.stringify(browserParameter)}`
					// #endif
					return;
				}
				console.log('infoMap',infoMap)

				const res = await this.$myRequest({
					url: 'api/member/updateInfo',
					method: 'post',
					data: infoMap,
				});

				if (res.newNumber == true) {
					if (res.spePushResut.pushSuccess == true) {
						var ss = res.spePushResut.skipUrl
						if (ss) {
							uni.setStorageSync('authorize', true)
							uni.setStorageSync('authorizeUrl', ss)
						}

					}

				}
				if (res.spePushResut) {
					return this.$refs.protocolRef.submit(res.spePushResut, infoMap);
				}

				if (res.spePushResut && res.spePushResut.companyName) {
					uni.redirectTo({
						url: `../home/home?companyName=${res.spePushResut.companyName}`
					})
				} else {
					uni.redirectTo({
						url: `../home/home`
					})
				}

			}


		},
		components: {
			axbCheckBox,
			pickers,
			protocol
		},
	}
</script>

<style>

</style>